package com.mc.provider.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 哥,写点什么吧！！
 *
 * @author LiuChunfu
 * @date 2018/2/7
 */
@RestController
public class MyController {


    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/test")
    public String test() {
        return "No-SUCCESS";
    }


    @Value("${spring.cloud.client.ipAddress}")
    private String ip;

    //@PostConstruct
    //public void test333() throws Exception {
    //    EurekaDiscoveryClient ori = (EurekaDiscoveryClient) discoveryClient;
    //    Field eurekaClientFiled = ReflectUtil.getField(EurekaDiscoveryClient.class, "eurekaClient");
    //    Object obj = ReflectUtil.getFieldValue(ori, eurekaClientFiled);
    //
    //    EurekaClient eurekaClient = (EurekaClient) AopTargetUtil.getTarget(obj);
    //    eurekaClient.registerEventListener(new EurekaEventListener() {
    //        @Override
    //        public void onEvent(EurekaEvent event) {
    //            System.out.println("6666666666");
    //        }
    //    });
    //}

    @GetMapping("/dis")
    public Object test22() throws Exception {

        //com.netflix.discovery.DiscoveryClient discoveryClient=(com.netflix.discovery.DiscoveryClient)eurekaClient;
        //boolean renew = ReflectUtil.invoke(eurekaClient, "renew");
        //return renew;
        return true;
    }

    @GetMapping
    public String ip() {
        return ip;
    }

    @RequestMapping(value = "/registered")
    public String getRegistered() {
        List<ServiceInstance> list = discoveryClient.getInstances("mic-provider");
        System.out.println(discoveryClient.getLocalServiceInstance());
        System.out.println("discoveryClient.getServices().size() = " + discoveryClient.getServices().size());

        for (String s : discoveryClient.getServices()) {
            System.out.println("services " + s);
            List<ServiceInstance> serviceInstances = discoveryClient.getInstances(s);
            for (ServiceInstance si : serviceInstances) {
                System.out.println("    services:" + s + "begin!");
                System.out.println("    services:" + s + ":getHost()=" + si.getHost());
                System.out.println("    services:" + s + ":getPort()=" + si.getPort());
                System.out.println("    services:" + s + ":getServiceId()=" + si.getServiceId());
                System.out.println("    services:" + s + ":getUri()=" + si.getUri());
                System.out.println("    services:" + s + ":getMetadata()=" + si.getMetadata());
                System.out.println("    services:" + s + "end!");
            }
        }

        System.out.println(list.size());
        if (list != null && list.size() > 0) {
            System.out.println(list.get(0).getUri());
        }
        return null;
    }

    @PostMapping("/aop")
    public void testAop() {
        System.out.println();
    }
}
